GM.BlockTypes = {
Barricade = { model = "models/hunter/blocks/cube05x05x05.mdl" }
}
GM.BlockList = {}

function GM:LoadBlocks()
	local num = 0
	for classname,class_table in pairs( scripted_ents.GetList() ) do
		if classname:sub( 1, 6 ):lower() == "block_" then
			num = num + 1
			GAMEMODE.BlockList[ classname ] = class_table.t
			if CLIENT then
				local spawnbutton = vgui.Create( "DButton" )
				spawnbutton:SetText( class_table.t.PrintName )
				spawnbutton:SetSize( rScrScale(40), rScrScale(40) )
				spawnbutton.DoClick = function()
					GAMEMODE:SetBlock( classname )
				end
				g_SpawnPanel:AddItem( spawnbutton )
			end
		end
	end
	
	if CLIENT then
		g_SpawnPanel:SetWide( num * rScrScale(40) + rScrScale(15) )
		g_SpawnPanel:SetPos( ScrW()/2 - g_SpawnPanel:GetWide()/2, ScrH()-g_SpawnPanel:GetTall() )
		g_SpawnPanel:InvalidateLayout()
	end
	
end

local cur_block = ""
local last_block_angle = Angle(0,0,0)
local last_block_pos = Vector(0,0,0)
local real_block_angle = Angle(0,0,0)
local real_block_pos = Vector(0,0,0)
local is_creating_block = false
function GM:SetBlock( str )
	if cur_block == str then
		GAMEMODE:CancelBlockCreation()
		return
	end
	
	if GAMEMODE.BlockList[ str ].CreateOnClick then
		RunConsoleCommand( "create_block", str, 0, 0, 0, 0, 0, 0, LocalPlayer():GetSyncVar( "optimistic" ) )
		return
	end
	
	cur_block = str
	
	if GAMEMODE.BlockList[ str ] then
		is_creating_block = true
	else
		LocalPlayer().Creator:SetNoDraw( true )
		is_creating_block = false
	end
end

function GM:GetBlock( str )
	return GAMEMODE.BlockList[ str ]
end

function GM:CreateBlock()
	-- Yes, I'm very optimistic.
	-- But having accurate positions and angles when a block is created is part of the fun.
	-- Fun > security.
	RunConsoleCommand( "create_block", cur_block, real_block_pos.x, real_block_pos.y, real_block_pos.z, real_block_angle.p, real_block_angle.y, real_block_angle.r, LocalPlayer():GetSyncVar( "optimistic" ) )
end

function GM:DrawBlock( block, pos, ang )

	if not block then return end
	if not GAMEMODE.BlockList[ cur_block ] then return end

	last_block_angle = LerpAngle( 0.15, last_block_angle, ang )
	last_block_pos = LerpVector( 0.5, last_block_pos, pos )
	real_block_angle = ang
	real_block_pos = pos
	
	--block:SetModel("models/hunter/blocks/cube05x05x05.mdl")
	
	render.SetColorModulation( 1,1,1 )
	local dist = last_block_pos:Distance( LocalPlayer():GetBuggyPos() )
	if dist > 75 or dist < 13 then
		render.SetColorModulation( 1,0,0 )
	end
	render.SetBlend(0.5)
	
	block:SetColor( 255,255,255,255 )
	block:SetNoDraw( false )
	GAMEMODE.BlockList[ cur_block ]:DrawBlockGhost( block, last_block_pos, last_block_angle )
	block:SetColor( 0,0,0,0 )
	
	render.SetBlend(1)
	render.SetColorModulation( 1,1,1 )
	
end

function GM:CancelBlockCreation()
	cur_block = ""
	LocalPlayer().Creator:SetNoDraw( true )
	is_creating_block = false
end

function GM:IsCreatingBlock()
	return is_creating_block
end
